from urllib import request
from selenium import webdriver
import urllib.request
import os
from selenium.webdriver.common.by import By


path = 'csdn'
if not os.path.exists(path):
    os.mkdir(path)
options = webdriver.EdgeOptions()  # 创建配置项对象
options.add_argument("--disable-blink-features=AutomationControlled")  # 添加配置项:跳过部分反爬虫程序
browser = webdriver.Edge(options=options)  # 将配置项添加到浏览器控制对象中
browser.get('https://book.douban.com/')
nums = browser.find_elements(By.XPATH, '//*[@id="content"]/div/div[1]/div[1]/div[2]/div[1]/div/ul[2]/li')
i = 0
for num in nums:
    i += 1
    img_url = num.find_element(By.XPATH, './div[1]/a/img').get_attribute('src')
    urllib.request.urlretrieve(img_url, path + 'csdn0' + str(i) + '.jpg')
print('图片获取完成')
browser.implicitly_wait(100)
browser.maximize_window()  # 最大化窗口
browser.quit()  # 关闭浏览器
